using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Curchy.PERTToolbox.FileAssociation;

namespace Curchy.PERTToolbox.Service
{
    public class InitializationToolbox
    {

        #region Singleton

        private static InitializationToolbox _instance;

        private InitializationToolbox() { }

        public static InitializationToolbox Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new InitializationToolbox();
                }
                return _instance;
            }
        }

        #endregion

        public bool IsAssociated
        {
            get
            {
                FileAssociationInfo fai = new FileAssociationInfo(".cprp");
                return fai.Exists;
            }
        }

        public void InitializeFileAssociation()
        {
            FileAssociationInfo fai = new FileAssociationInfo(".cprp");
            if (!fai.Exists)
            {
                fai.Create("PERT Toolbox", PerceivedTypes.None);
                fai.ContentType = "application/cprp";
                fai.ProgID = "PERT Toolbox";
            }

            ProgramAssociationInfo pai = new ProgramAssociationInfo(fai.ProgID);
            pai.Create("PERT Toolbox Project", new ProgramVerb("open", Application.ExecutablePath + " /file=%1"));
            pai.DefaultIcon = new ProgramIcon(Application.ExecutablePath, 0);
            
        }

        public void RemoveFileAssociation()
        {
            
            FileAssociationInfo fai = new FileAssociationInfo(".cprp");
            ProgramAssociationInfo pai = new ProgramAssociationInfo(fai.ProgID);
            
            if (pai.Exists)
            {
                pai.RemoveVerb("open");
                pai.Delete();
            }

            if (fai.Exists)
                fai.Delete();
        }

    }

}
